package cn.touna.jss.modules.custom.tobaccouser;

import cn.touna.jss.common.dict.entity.SysDict;
import cn.touna.jss.common.dict.service.DictService;
import cn.touna.jss.common.properties.disconf.TobaccoProperties;
import cn.touna.jss.modules.adapter.ApiMapping;
import cn.touna.jss.modules.custom.tobaccouser.entity.TobaccoUser;
import cn.touna.jss.modules.custom.tobaccouser.service.TobaccoService;
import cn.touna.risk.api.domain.Response;
import cn.touna.risk.api.enums.SystemErrorCodeEnum;
import cn.touna.risk.threadpool.common.util.ThreadPoolUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.rocketmq.client.producer.MQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.touna.loan.log.service.TracerContextUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.UUID;


/**
 * @author: xiaoli
 * @date: 2019/3/11 17:48
 * @Desc:烟草用户查询
 **/
@Service
public class RetailUserService {


    private static final Logger LOGGER = LogManager.getLogger(RetailUserService.class);


    @Autowired
    TobaccoService tobaccoService;

    @Autowired
    DictService dictService;

    @Autowired
    MQProducer mqProducer;

    @Autowired
    TobaccoProperties tobaccoProperties;


    @ApiMapping(value = "InnerData.getRetailerData", desc = "中国烟草.查询烟草用户数据")
    public Response getRetailerData(TobaccoUser entity) {
        Response response = new Response();
        try {
            JSONObject result = tobaccoService.getTobaccoData(entity);
            if (result == null) {
                return response.fail(SystemErrorCodeEnum.CRS_NO_DATA_FOUND.getErrorCode(), "非烟草用户");
            }
            return response.success(SystemErrorCodeEnum.SYSTEM_REQUEST_SUCC, result);
        } catch (Exception e) {
            return response.fail(SystemErrorCodeEnum.JSS_CALL_EXTSYSTEM_ERROR);
        }

    }


    @ApiMapping(value = "InnerData.sendCrmData", desc = "中国烟草.查询烟草用户数据")
    public Response sendCrmData(JSONObject jsonObject) {
        Response response = new Response();
        try {
            ThreadPoolUtil.submit(this,"sendMqToCrm",JSONObject.toJavaObject(jsonObject,TobaccoUser.class));
            return response.success(SystemErrorCodeEnum.SYSTEM_REQUEST_SUCC);
        } catch (Exception e) {
            return response.fail(SystemErrorCodeEnum.JSS_CALL_EXTSYSTEM_ERROR);
        }
    }


    private void sendMqToCrm(TobaccoUser entity) {
        List<TobaccoUser> list = tobaccoService.queryTobaccoUserList(entity);
        if(CollectionUtils.isEmpty(list)){
            return ;
        }
        for (TobaccoUser tobaccoUser : list) {
            JSONObject result = new JSONObject();
            try {
                result.put("applyNo", TracerContextUtil.getTraceId());
                result.put("channelCode", "zgyc001");
                result.put("custName", tobaccoUser.getName());
                result.put("idCard", tobaccoUser.getIdCard());
                result.put("phone", tobaccoUser.getMobile());
                Message message = new Message();
                message.setKeys(UUID.randomUUID().toString());
                message.setTopic(tobaccoProperties.getCrmTopic());
                message.setBody(result.toJSONString().getBytes("UTF-8"));
                SendResult sendResult = mqProducer.send(message);
                LOGGER.info("发送烟草用户数据到crm" + sendResult.toString());
            } catch (Exception e) {
                e.printStackTrace();
                LOGGER.info("发送烟草用户数据到crm异常" + e.getMessage());
            }
        }
        LOGGER.info("发送烟草用户数据到crm条数" + list.size());
    }


    @ApiMapping(value = "InnerData.queryRetailerUser", desc = "中国烟草.是否为烟草用户")
    public Response queryRetailerUser(TobaccoUser entity) {
        Response response = new Response();
        JSONObject result = new JSONObject();
        String operCode = "NO_DATA";
        boolean isExist = tobaccoService.checkUser(entity);
        if (isExist) {
            List<SysDict> list = dictService.queryListByDataType("oper_code", entity.getChannel());
            if (!CollectionUtils.isEmpty(list)) {
                SysDict dict = list.get(0);
                operCode = dict.getDataValue();
            }
        }

        result.put("operCode", operCode);
        result.put("isUser", isExist);
        return response.success(SystemErrorCodeEnum.SYSTEM_REQUEST_SUCC, result);
    }


}

























